package create.com.modelproaction.activity.actionactivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import create.com.modelproaction.R;
import create.com.modelproaction.entity.bluetoothdevice;

/***
 * 蓝牙功能
 *
 * https://devbox.cn/p/112384709
 */
public class BlueToothActivity extends AppCompatActivity {

    private Button btn_blu;

    private BluetoothAdapter adapter;

//    private MyReceiver receiver;

    private ListView mListView;

    private List<bluetoothdevice> mDevices;
    private BluetoothSocket mSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue_tooth);

        initView();
    }

    private void initView() {
        btn_blu=findViewById(R.id.btn_blu);

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter==null){
            Toast.makeText(this, "该手机不支持蓝牙功能", Toast.LENGTH_LONG).show();
        }
        if (bluetoothAdapter.isEnabled()) {
            // 蓝牙已启用，执行相应操作
            Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(intent);
        } else {
            // 蓝牙未启用，执行相应操作
        }

        IntentFilter filter = new IntentFilter();
        // 注册扫描开始的广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        // 注册扫描结束的广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        // 注册扫描已经找到设备的广播
        filter.addAction(BluetoothDevice.ACTION_FOUND);
//        receiver = new MyReceiver();
//        registerReceiver(receiver, filter);
//        startBlueServer();
    }
    /**
     * 启动蓝牙服务
     */
//    private void startBlueServer()
//    {
//        new Thread()
//        {
//            @Override
//            public void run()
//            {
//                while (true)
//                {
//                    try
//                    {
//                        BluetoothServerSocket serverSocket = adapter
//                                .listenUsingRfcommWithServiceRecord(
//                                        "蓝牙服务",
//                                        UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
//                        mSocket = serverSocket.accept();//阻塞
//                        BlueToothHandler.getInstance().setSocket(mSocket);
//                    }
//                    catch (IOException e)
//                    {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }.start();
//    }
//
//
//    private void initListView()
//    {
//        mDevices = new ArrayList<bluetoothdevice>();
//        mDeviceAdapter = new  DeviceAdapter(this, mDevices);
//        mListView.setAdapter(mDeviceAdapter);
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
//        {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view,
//                                    int position, long id)
//            {
//                try
//                {
//                    mSocket = mDevices
//                            .get(position)
//                            .createRfcommSocketToServiceRecord(
//                                    UUID.fromString("997f1b20-b4a0-45ea-a7dd-b2097299b1f6"));
//                    mSocket.connect();//阻塞
//                    Toast.makeText(this.getClass(), "连接成功",
//                            Toast.LENGTH_SHORT).show();
//                    BlueToothHandler.getInstance().setSocket(mSocket);
//                }
//                catch (IOException e)
//                {
//                    e.printStackTrace();
//                }
//
//            }
//        });
//    }
//
//
//    public class MyReceiver extends BroadcastReceiver
//    {
//
//        @Override
//        public void onReceive(Context context, Intent intent)
//        {
//            String action = intent.getAction();
//            if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
//            {
//                Log.i("info", "开始扫描");
//            }
//            else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
//            {
//                Log.i("info", "扫描结束");
//            }
//            else if (action.equals(BluetoothDevice.ACTION_FOUND))
//            {
//                BluetoothDevice device = intent
//                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//                Log.i("info", device.getName() + ":" + device.getAddress());
//                if (mDevices != null)
//                {
//                    for (int i = 0; i < mDevices.size(); i++)
//                    {
//                        if (device.getAddress()
//                                .equals(mDevices.get(i).getNo()))
//                        {
//                            return;
//                        }
//                    }
//                    mDevices.add(device);
//                    mDeviceAdapter.notifyDataSetChanged();
//                }
//            }
//        }
//
//    }
}
